package cn.net.ecode.tools.ecodeplatformmaker.util;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class OperateXml {

	public static void saveConn(Map map) {
		try {
			if (new File(System.getProperty("user.dir") + "/Connection.xml")
					.exists()) {
				if("".equals(map.get("uuid")))
				{
					OperateXml.addNewDocumentFile(System.getProperty("user.dir")
							+ "/Connection.xml", map);
				}
				else
				{
					OperateXml.changeNodeFromXmlFile(System.getProperty("user.dir")
							+ "/Connection.xml", map);
				}
				
			} else {
				OperateXml.createNewDocumentFile(System.getProperty("user.dir")
						+ "/Connection.xml", map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			// System.out.println(Thread.currentThread().getContextClassLoader()
			// .getResource(""));
			 System.out.println(OperateXml.class.getClassLoader().getResource(""));
			 System.out.println(ClassLoader.getSystemResource(""));
			 System.out.println(ClassLoader.getSystemResource(""));
			// System.out.println(OperateXml.class.getResource(""));
			// System.out.println(OperateXml.class.getResource("/")); //
			// Class文件所在路径
			System.out.println(new File("/").getAbsolutePath());
			// System.out.println(System.getProperty("user.dir"));
			/*OperateXml
					.changeNodeFromXmlFile("H:\\Tools\\j2ee_eclipse\\eclipse\\Connection.xml",new HashMap());*/
			// ReadFile.changeNodeFromXmlFile("c:\\test.xml");
			// ReadFile.createNewDocumentFile("c:\\test2.xml");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static List readAllNodesFromXmlFile(String path) throws Exception {
		List list = new ArrayList();
		
		SAXReader reader = new SAXReader();
		File file = new File(path);
		Document document = reader.read(file);// 读取XML文件
		Element root = document.getRootElement();// 得到根节点
		Iterator it = root.elementIterator();
		while (it.hasNext()) { // 第一级
			Element el = (Element) it.next();
			Iterator it2 = el.elementIterator();
			while (it2.hasNext()) // 第二级
			{
				Map map = new HashMap();
				Element el2 = (Element) it2.next();
				map.put("uuid", el2.attributeValue("uuid"));
				map.put("isdefault", el2.attributeValue("default"));
				Iterator it3 = el2.elementIterator();
				while (it3.hasNext()) // 第三级
				{
					Element el3 = (Element) it3.next();
					map.put(el3.getName(), el3.getText());
				}
				list.add(map);
			}

		}
		return list;
	}

	public static void changeNodeFromXmlFile(String path,Map map) throws Exception {
		XMLWriter writer = null;// 声明写XML的对象
		SAXReader reader = new SAXReader();
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("GB2312");
		File file = new File(path);
		Document document = reader.read(file);// 读取XML文件
		Element root = document.getRootElement();// 得到根节点
		Iterator it = root.elementIterator();
		while (it.hasNext()) { // 第一级
			Element el = (Element) it.next();
			Iterator it2 = el.elementIterator();
			while (it2.hasNext()) // 第二级
			{
				Element el2 = (Element) it2.next();
				if(map.get("uuid").equals(el2.attributeValue("uuid")))
				{
					Iterator it3 = el2.elementIterator();
					while (it3.hasNext()) // 第三级
					{
						Element el3 = (Element) it3.next();
						el3.setText((String)map.get(el3.getName()));
						writer = new XMLWriter(new FileWriter(file), format);
						writer.write(document);
						writer.close();
					}
				}
			}

		}
	}

	public static void createNewDocumentFile(String path, Map map)
			throws Exception {
		File file = new File(path);
		XMLWriter writer = null;// 声明写XML的对象
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("GB2312");
		Document _document = DocumentHelper.createDocument();
		Element _root = _document.addElement("root");
		_root.addAttribute("xmlns:xsi",
				"http://www.w3.org/2001/XMLSchema-instance");
		Element first = _root.addElement("Connections");
		Element second = first.addElement("connection");
		second.addAttribute("default", (String) map.get("default"));
		second.addAttribute("uuid", java.util.UUID.randomUUID().toString());
		second.addElement("ip").setText((String) map.get("ip"));
		second.addElement("port").setText((String) map.get("port"));
		second.addElement("sid").setText((String) map.get("sid"));
		second.addElement("account").setText((String) map.get("account"));
		second.addElement("password").setText((String) map.get("password"));
		writer = new XMLWriter(new FileWriter(file), format);
		writer.write(_document);
		writer.close();
	}

	public static void addNewDocumentFile(String path, Map map)
			throws Exception {
		File file = new File(path);
		SAXReader reader = new SAXReader();
		XMLWriter writer = null;// 声明写XML的对象
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("GB2312");
		Document _document = reader.read(file);
		Element root = _document.getRootElement();// 得到根节点
		Iterator it = root.elementIterator();
		while (it.hasNext()) { // 第一级
			Element el = (Element) it.next();
			Element second = el.addElement("connection");
			second.addAttribute("default", (String) map.get("default"));
			second.addAttribute("uuid", java.util.UUID.randomUUID().toString());
			second.addElement("ip").setText((String) map.get("ip"));
			second.addElement("port").setText((String) map.get("port"));
			second.addElement("sid").setText((String) map.get("sid"));
			second.addElement("account").setText((String) map.get("account"));
			second.addElement("password").setText((String) map.get("password"));
			writer = new XMLWriter(new FileWriter(file), format);
			writer.write(_document);
			writer.close();
		}
	}
}
